<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataJbfa } from "@/api/search";
import { addOrUpdateJbfa } from "@/api/addOrUpdate";

const editorRef = shallowRef();
const editorRef1 = shallowRef();
const editorRef2 = shallowRef();

const valueHtml = ref(
  "<p>主要明确情报信息保障、综合保障、政治工作的任务区分和方法要求等内容。<br/> 情报信息保障由战时城市情报信息部组织实施。主要任务：①情报获取，搜集处理上级提供的战略情报、战场态势、实时空情、敌特活动等情报信息，以及本级侦搜的对空观察、低空预警雷达探测、重要目标警戒等情报信息；②预警报知，根据实际情况依令发放早期预警、空袭预警、解除空袭警报；③指挥所信息节点开设，组织开设各级各类指挥所信息节点；④通信网络组织联接，组织光纤、短波、卫星、北斗导航等通信网的网络联接等；⑤指控系统调配部署，组织各级指挥所指控系统的设备调配、软件部署，以及系统备份等；⑥信息防护，组织数据、网络、平台安全防护和容灾备份等。<br/>任务区分：XX由XX部门负责，XX由XX部门负责，XX由XX部门负责。<br/>方法要求：XX</p>"
);
const valueHtml1 = ref(
  "<p>综合保障由XX组织实施。主要任务：①XX；②XX；③XX；④XX；⑤XX；⑥XX。<br/>任务区分：XX由XX部门负责，XX由XX部门负责，XX由XX部门负责。<br/>方法要求：XX</p>"
);

const valueHtml2 = ref(
  "<p>政治工作由XX组织实施。主要任务：①XX；②XX；③XX；④XX；⑤XX；⑥XX。<br/>任务区分：XX由XX部门负责，XX由XX部门负责，XX由XX部门负责。<br/>方法要求：XX</p>"
);

const toolbarConfig = {};
const toolbarConfig1 = {};
const toolbarConfig2 = {};
const editorConfig = {
  placeholder: "请输入内容...",
};

const mode = ref("default");
const mode1 = ref("default");
const mode2 = ref("default");

// 加载html
const initHtml = () => {
  searchDataJbfa({ m: "相关保障" }).then((res) => {
    if (res.code !== 200) return ElMessage.error(res.msg);
    const { data } = res;
    valueHtml.value =
      data.find((x) => x.k === "情报信息保障任务区分方法要求")?.v || "";
    valueHtml1.value =
      data.find((x) => x.k === "综合保障任务区分方法要求")?.v || "";
    valueHtml2.value =
      data.find((x) => x.k === "政治工作任务区分方法要求")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};
const handleCreated1 = (editor) => {
  editorRef1.value = editor; // 记录 editor 实例，重要！
};
const handleCreated2 = (editor) => {
  editorRef2.value = editor; // 记录 editor 实例，重要！
};

// 保存
const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateJbfa({
      m: "相关保障",
      data: [
        {
          k: "情报信息保障任务区分方法要求",
          v: valueHtml.value,
        },
        {
          k: "综合保障任务区分方法要求",
          v: valueHtml1.value,
        },
        {
          k: "政治工作任务区分方法要求",
          v: valueHtml2.value,
        },
      ],
    })
      .then(() => {
        ElMessage.success("操作成功");
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});

onBeforeUnmount(() => {
  const editor = editorRef.value;
  const editor1 = editorRef1.value;
  const editor2 = editorRef2.value;
  if (!editor) return;
  if (!editor1) return;
  if (!editor2) return;
  editor.destroy();
  editor1.destroy();
  editor2.destroy();
});
</script>
<template>
  <div>
    <FirstTitle name="情报信息保障任务区分、方法要求" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <FirstTitle name="综合保障任务区分、方法要求" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef1"
        :defaultConfig="toolbarConfig1"
        :mode="mode1"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml1"
        :defaultConfig="editorConfig"
        :mode="mode1"
        @onCreated="handleCreated1"
      />
    </div>

    <FirstTitle name="政治工作任务区分、方法要求" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef2"
        :defaultConfig="toolbarConfig2"
        :mode="mode2"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml2"
        :defaultConfig="editorConfig"
        :mode="mode2"
        @onCreated="handleCreated2"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss">
.zddj {
  font-size: 14px;
}
</style>